using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.IO;
namespace ArchSoft
{
    public partial class ReportFrm : Form
    {
        private string _folderPath;
        private string _folderName;

        string PageSize = "A2";

        DataSet1 ds = new DataSet1();
        DataSet1.DrawingRow row;

        DataSet1.DevRow row1;
        
        CrystalReport1 objRpt = new CrystalReport1();

        public ReportFrm()
        {
            InitializeComponent();
        }

        public string FolderPath
        {
            set { _folderPath = value; }
        }

        public string FolderName
        {
            set { _folderName = value; }
        }

        public string GetTitle()
        {
            string Pat = Directory.GetParent(this._folderPath).ToString() + Path.DirectorySeparatorChar + ProjectManager.PROJECT_INF_FILE;
            if (File.Exists(Pat))
            {
                //Load Noi dung File Dinh nghia(neu ton tai)!!!               
                System.Data.DataTable tbConsl = null;

                DataSet ds = new DataSet();
                ds.ReadXml(Pat);
                tbConsl = ds.Tables[0];
                //Load Consultant!
                if (tbConsl != null)
                {
                    //Gene!!    .TrimEnd(new char[] { '\0' });
                    Pat = EncodeDecode.GetDecoded(tbConsl.Rows[5][1].ToString()).Trim();
                    PageSize = EncodeDecode.GetDecoded(tbConsl.Rows[20][1].ToString()).Trim().Split(' ')[0];
                    if (Pat == "")
                        Pat = "NO NAME";
                }
                else
                    Pat = "NO NAME";
            }
            else
            {
                Pat = "NO NAME";
            }
            return Pat;
        }

        string Tbx_Dev_Appr;
        string Tbx_Dev_chck;
        string Tbx_Dev_Des;
        string Tbx_Dev_Drw;

        public void AddDev()
        {
            string Pat = Directory.GetParent(this._folderPath).ToString() + Path.DirectorySeparatorChar + ProjectManager.PROJECT_INF_FILE;
            if (File.Exists(Pat))
            {
                System.Data.DataTable tbConsl = null;
                DataSet ds = new DataSet();
                ds.ReadXml(Pat);
                tbConsl = ds.Tables[0];
                if (tbConsl != null)
                {
                     Tbx_Dev_Appr = EncodeDecode.GetDecoded(tbConsl.Rows[15][1].ToString());
                     Tbx_Dev_chck = EncodeDecode.GetDecoded(tbConsl.Rows[16][1].ToString());
                    Tbx_Dev_Des  = EncodeDecode.GetDecoded(tbConsl.Rows[17][1].ToString());
                    Tbx_Dev_Drw  = EncodeDecode.GetDecoded(tbConsl.Rows[18][1].ToString());

                    //row[0] = Tbx_Dev_Appr;
                    //row[1] = Tbx_Dev_chck;
                    //row[2] = Tbx_Dev_Des;
                    //row[3] = Tbx_Dev_Drw;
                }   
            }
        }

        public void NewDev()
        {
            AddDev();
            row1 = ds.Dev.NewDevRow();

            row1[0] = Tbx_Dev_Appr;
            row1[1] = Tbx_Dev_chck;
            row1[2] = Tbx_Dev_Des;
            row1[3] = Tbx_Dev_Drw;

            ds.Dev.AddDevRow(row1);
            
        }

        public void NewLineForDrawing(string DrawingName, string Scale, int order)
        {
            try
            {
                //string[] names = DrawingName.Split('_');
                //names[1] = Path.GetFileNameWithoutExtension(names[1]);


                //row = reportDT.NewRow();

                //row["Element"] = _folderName;
                //row["ID"] = names[0].Split('-')[0];
                //row["Number"] = names[0].Split('-')[1];
                //row["Name"] = names[1];
                //row["Scale"] = Scale;
                //// row["Size"] = PageSize;
                //row["Size"] = PageSize;

                //reportDT.Rows.Add(row);

                string[] names = DrawingName.Split('_');
                names[1] = Path.GetFileNameWithoutExtension(names[1]);

                row = ds.Drawing.NewDrawingRow();
                row[0] = _folderName;
                row[1] = names[0].Split('-')[0] + "-" + names[0].Split('-')[1];
                //row[2] = names[0].Split('-')[1];
                row[3] = names[1];
                row[4] = Scale;
                row[5] = PageSize;

                ds.Drawing.AddDrawingRow(row);
                
            }
            catch (Exception ex)
            {

            }
        }

        public void NewBlankForDrawing()
        {
            row = ds.Drawing.NewDrawingRow();
            row[0] = string.Empty;
            row[1] = string.Empty;
            //row[2] = names[0].Split('-')[1];
            row[3] = string.Empty;
            row[4] = string.Empty;
            row[5] = string.Empty;

            ds.Drawing.AddDrawingRow(row);
        }

        public void SetDataToDataSet()
        {

        }

        private void ReportFrm_Load(object sender, EventArgs e)
        {
            //objRpt.DataDefinition.FormulaFields[0].Text = ds.Drawing.Columns["Name"].ToString();
            objRpt.SetDataSource(ds);
            crystalReportViewer1.ReportSource = objRpt;
        }
    }
}